package server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import plug.result.ReqPage;
import plug.result.RetResponse;
import plug.result.RetResult;
import server.dao.AccountDao;
import server.model.entity.Account;
import server.model.entity.Tenant;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.UUID;

/**
 * @description:
 * @author: 许哲
 * @createTime: 2021/11/21 17:11
 * @version: 1.0
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("v1/api/account")
public class AccountController {
    @Autowired
    private AccountDao accountDao;

    @PostMapping("/all")
    public RetResult all(@RequestBody ReqPage reqPage) {
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        Account entity = JSON.parseObject(req.toJSONString(), Account.class);
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("account", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Account> example = Example.of(entity, matcher);
        Page<Account> pages = accountDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }

    @PostMapping("/save")
    public RetResult save(@RequestBody Account account) {
        accountDao.save(account);
        return RetResponse.makeOKRsp();
    }
}
